public class MySingleList {

    public Node head;

    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }


    }

    public void disPlay(Node newHead) {
        Node cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void disPlay() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void createList() {
        Node node1 = new Node(14);
        Node node2 = new Node(54);
        Node node3 = new Node(36);
        Node node4 = new Node(24);
        head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
    }

    public int size() {
        Node cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int data) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == data) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public Node findKthToTail(int k) {
        if (k <= 0 || head == null) {
            return null;
        }
        Node fast = head;
        Node slow = head;
        while (k-1 != 0) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public Node middleNode() {
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public void remove(int key) {
        if (head == null) {
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }
        Node cur = findKey(key);
        if (cur == null) {
            return;
        }
        Node del = cur.next;
        cur.next = del.next;

    }

    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        Node prev = head;
        Node cur = head.next;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur =cur.next;
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    private Node findKey(int key) {
        Node cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    public void addIndex(int index,int data) throws IndexOutOfException{
        checkIndex(index);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;

    }

    private Node findIndex(int index) {
        Node cur = head;
        int count = 0;
        while (count != (index-1)) {
            count++;
            cur = cur.next;
        }
        return cur;
    }

    public void checkIndex(int index) throws IndexOutOfException{
        if (index < 0 || index > size()) {
            throw new IndexOutOfException("Index的位置不合法！！！");
        }
    }

    public Node reverseList() {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        Node cur = head.next;
        head.next = null;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    public  Node mergeTwoList(Node head1,Node head2){
        Node nextHead = new Node(0);
        Node tmp = nextHead;
        while (head1 != null && head2 != null) {
            if (head1.val  < head2.val) {
                tmp.next = head1;
                head1 = head1.next;
            }else {
                tmp.next = head2;
                head2 = head2.next;
            }
            tmp = tmp.next;
        }
        if (head1 != null) {
            tmp.next = head1;
        }
        if (head2 != null) {
            tmp.next = head2;
        }
        return nextHead.next;
    }

    public boolean hasCycle1() {
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }
    public Node hasCycle2() {
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        slow = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    public boolean isPalindrome() {
        if (head == null) {
            return false;
        }
        if (head.next == null) {
            return true;
        }
        Node fast = head;
        Node slow = head;
        //1.找到中间结点
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.翻转中间结点后面的链表
        Node cur = slow.next;
        while (cur != null) {
            Node nextHead = cur.next;
            cur.next = slow;
            slow = cur;
            cur = nextHead;
        }
        //3.head从前往后，slow从后往前遍历
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public Node partition(int x) {
        Node bs = null;
        Node be = null;
        Node as = null;
        Node ae = null;
        Node cur = head;
        while (cur != null) {
            if(cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }

            }
            cur =cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

}
